#include"TcpServer.hpp"
#include"Socket.hpp"
#include"Accepter.hpp"
#include<memory>
#include<iostream>

using namespace std;
using namespace Network;

static const int gbacklog=32;

void Usage(string process)
{
    cerr<<"Usage:\n"<<process<<"    port"<<endl;
}

void Test(Connection* conn)
{
    cout<<"get event,sockfd is"<<conn->SockFd()<<endl;
}


//./epoll_server 8888
int main(int argc,char* argv[])
{
    if(argc!=2)
    {
        Usage(argv[0]);
        return 1;
    }

    uint16_t port=stoi(argv[1]);
    //1.创建listensock
    unique_ptr<Socket> listensock=make_unique<TcpSocket>();
    listensock->BuildlistenSocketMethod(port,gbacklog);

    
    //2.创建tcpserver
    unique_ptr<TcpServer> svr=make_unique<TcpServer>();
    unique_ptr<Accepter> accepter=make_unique<Accepter>();

    auto listen_recver=bind(&Accepter::AccepterConnection,accepter.get(),std::placeholders::_1);
    //


    //4.构建listen对应的connection 添加到tcpserver中
    Connection* conn=ConnectionFatory::BuildListenConnection(listensock->GetSockfd(),listen_recver,EPOLLIN|EPOLLET,svr.get());
    svr->AddConnection(conn);
    //5.开始事件派发
    svr->Dispatcher();//开始事件派发
    return 0;
}